<script setup>
import { TreeListAPI } from '@/api/mold/requisition'
import useUserStore from '@/store/modules/user'
import { GetDeptUserAPI } from '@/api/common/index.js'

/**
 * 用户信息
 */
const useUser = useUserStore()

/**
 * 子传父
 */
const emit = defineEmits()
const { proxy } = getCurrentInstance()
/**
 * 字典
 */
const { sys_alarm_way } = proxy.useDict('sys_alarm_way')

// 父传子
const props = defineProps({
  isShow: {
    type: Boolean,
    default: false,
  },
  //   传入数据 用于回显
  data: {
    type: Object,
    default: () => {},
  },
  // 是否禁用
  disabled: {
    type: Boolean,
    default: false,
  },
  //   传入数据 弹窗标题
  title: {
    type: String,
    default: '新增',
  },
})

/**
 * 人员下拉
 */
const useList = ref([])
/**
 * 安装地点
 */
const dataTree = ref([])
/**
 * 列表
 */
const receiveList = ref([])

/**
 * 列表枚举
 */
const column = ref([
  { label: '报废人', prop: 'scrapBy', width: '250', required: true },
  { label: '模具编码', prop: 'mouldCode', width: '150' },
  { label: '模具名称', prop: 'mouldName', width: '150' },
  { label: '模具类型', prop: 'mouldType', width: '150' },
  { label: '模具型号', prop: 'mouldSpecification', width: '200' },
  { label: '制造厂商', prop: 'factoryName', width: 'auto' },
])

/**
 * 初始化数据
 */
const initData = () => {
  if (props.data) {
    receiveList.value = props.data
  }
}

initData()

/**
 * 人员
 */
const getList = async () => {
  try {
    const { data: res } = await GetDeptUserAPI()

    useList.value = res
  } finally {
  }
}

getList()

/**
 * 安装地点
 */
const treeLisDdl = async () => {
  const { data: res } = await TreeListAPI()

  dataTree.value = res
}
treeLisDdl()

/**
 * 提交方法
 */
const submit = async () => {
  // 校验填写 明细项
  const resukt = receiveList.value.every((t) => t.scrapBy)
  if (!resukt) {
    return proxy.warningMsg('报废人为必填!')
  }

  return receiveList.value
}
// 导出方法
defineExpose({ submit })
</script>
<template>
  <p-table-form
    keyId="id"
    :column="column"
    stripe
    border
    :data="receiveList"
    height="500"
    highlight-current-row
    @change="handleSelectionChange"
  >
    <template #columnCell="{ column, record }">
      <el-tree-select
        v-if="column.label == '报废人'"
        v-model="record.scrapBy"
        :data="useList"
        :render-after-expand="false"
        clearable
        style="width: 240px"
        :props="{
          children: 'children',
          label: 'label',
          value: 'id',
        }"
      />
    </template>
  </p-table-form>
</template>

<style lang="scss" scoped>
.rk ::v-deep .el-form-item {
  width: 100% !important;
}
</style>
